package com.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * Created by Administrator on 2020/4/16 0016.
 */
@Configuration
public class TopicRabbitConfig {
    //绑定键
    public final static String systemInfo = "systemInfo";
    public final static String orderQuestions = "orderQuestions";

    @Bean
    public Queue systemInfoQueue() {
        return new Queue(TopicRabbitConfig.systemInfo);
    }

    @Bean
    public Queue orderQuestionsQueue() {
        return new Queue(TopicRabbitConfig.orderQuestions);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }


    //将systemInfoQueue和topicExchange绑定,而且绑定的键值为systemInfo
    //这样只要是消息携带的路由键是systemInfo,才会分发到该队列
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(systemInfoQueue()).to(exchange()).with(systemInfo);
    }

    //将orderQuestionsQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则orderQuestions.#
    // 这样只要是消息携带的路由键是以orderQuestions.开头,都会分发到该队列
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(orderQuestionsQueue()).to(exchange()).with("orderQuestions.#");
    }

    @Bean
    public TaskScheduler taskScheduler(){
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        taskScheduler.initialize();
        return taskScheduler;
    }
}
